@echo off

REM Go to the directory where this BAT file is located.
REM We need to do this when launched from the windows explorer.
CD %~d0%~p0

REM Set the path for BennuGD and the rest of the needed libraries
SET PATH=%CD%\bin_win32;%CD%\bin_win32\lib;%CD%\bin_win32\modules;%PATH%
SET CONFIGDIR=%APPDATA%\Kantaoke\

REM If the songs dir doesn't exist, create it and copy some songs there
SET OLDPWD=%CD%
CD %APPDATA%
IF NOT EXIST Kantaoke\NUL (
	MKDIR Kantaoke\Songs
	CD %OLDPWD%
	XCOPY /E Songs "%CONFIGDIR%\Songs"
)
CD %OLDPWD%
	
REM Compile and, if succesful, run
bgdc -DDEBUG -DWIN32 main.prg
IF EXIST main.dcb bgdi main

GOTO:EOF

#!/bin/bash

# Set to 1 for debug builds, 0 for release
DEBUG=1
OS=`uname`
SOURCEFILE="main"

# Guess where are we and go to that dir
CURDIR=$(dirname $0)
cd $CURDIR
CURDIR=$(pwd)

# If given the name of a file, run it
if [ -f extra/$1_sample.prg ]; then
	echo Will run test code for $1, instead of the normal code
	SOURCEFILE="extra/$1_sample"
fi

# Create the paths to the BennuGD binaries and to
# the needed libraries 
if [[ $OS = "Darwin" ]]; then
	BINDIR=$CURDIR/bin_osx
	CONFIGDIR=$HOME/Library/Kantaoke/
else
    BINDIR=$CURDIR/bin
	CONFIGDIR=$HOME/.Kantaoke/
fi
LIBDIR=$BINDIR/lib
SONGSDIR=$CONFIGDIR/Songs/

# Create the config and songs dir, in case they don't exist
# and check if there are any songs there, otherwise link
# the bundled ones there
#
# This should only be done during the first run
if [ ! -d $SONGSDIR ]; then
	echo "$SONGSDIR not found! Creating it"
	mkdir -p $SONGSDIR
	for i in Songs/*; do
		cp -r $i $CONFIGDIR/$i
	done
fi

# Set the required env vars, giving our bundled version
# of BennuGD priority over a (possibly) installed version.
export PATH=$BINDIR:$PATH
if [[ $OS = "Darwin" ]]; then
	export DYLD_LIBRARY_PATH=$LIBDIR::$DYLD_LIBRARY_PATH:$BINDIR/modules
	export PANGO_RC_FILE=$BINDIR/etc/pangorc
	export FONTCONFIG_PATH=$BINDIR/etc/fonts
else
	export LD_LIBRARY_PATH=$LIBDIR
fi

# Delete the old DCB and compile (if needed)
if [ $DEBUG = 1 ]; then
	if [ -f $SOURCEFILE.dcb ]; then
		rm $SOURCEFILE.dcb
	fi

	if [ $OS = "Darwin" ]; then
		bgdc -DDEBUG -DOSX $SOURCEFILE.prg
	else
		bgdc -DDEBUG $SOURCEFILE.prg
	fi
fi

# And run
if [ -f $SOURCEFILE.dcb ]; then
	bgdi $SOURCEFILE
fi
